package com.wanfajie.smsdistribute

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.wanfajie.smsdistribute.util.refreshToast

class MyApplication : Application() {

    companion object {
        private lateinit var instance: Application

        val context: Context get() = instance.baseContext
        val appContext: Context get() = instance
        val app: Application get() = instance

        private const val FIRST_START_PREF_KEY = "isFirstStart"
        private const val APP_PREF_NAME = "application"

        private val basePref: SharedPreferences by lazy {
            context.getSharedPreferences(APP_PREF_NAME, Context.MODE_PRIVATE)
        }

        val isFirstStart: Boolean by lazy {
            val r = basePref.getBoolean(FIRST_START_PREF_KEY, true)

            if (r) basePref.edit()
                    .putBoolean(FIRST_START_PREF_KEY, false)
                    .apply()

            r
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this

        refreshToast(this)
    }
}